package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ListActivity extends AppCompatActivity {

    ListView lvContext;

    int[] ids=new int[]{
            R.mipmap.qing, R.mipmap.ic_launcher, R.mipmap.ic_launcher,
            R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher
    };
    String[] name=new String[]{
            "青花瓷","江南","333","444","555","666"
    };
    int[] datas=new int[]{
            R.raw.qhc, R.raw.jiang, R.raw.qhc,
            R.raw.qhc, R.raw.qhc, R.raw.qhc
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        lvContext=findViewById(R.id.lvContext);

        List<Map<String,String>> list=new ArrayList<>();
        for (int m=0;m<ids.length;m++){
            Map<String,String> map=new HashMap<>();
            map.put("imgKey",String.valueOf(ids[m]));
            map.put("nameKey",name[m]);
            list.add(map);
        }
        lvContext.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(getApplicationContext(),PlayActivity.class);
                for (int i = 0; i < name.length; i++) {
                    if (position==i){
                        intent.putExtra("data",datas[i]);
                    }
                }
                startActivity(intent);
            }
        });

        String[] from=new String[]{"imgKey","nameKey"};
        int[] to=new int[]{R.id.ivContext,R.id.tvContext};
        SimpleAdapter adapter=new SimpleAdapter(ListActivity.this,list,R.layout.context_item,from,to);
        lvContext.setAdapter(adapter);
    }
}
